package com.globalwave.common.datagridrenderer
{
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.events.FlexEvent;
	public class CheckBoxItemRenderer extends CheckBox
	{
		public function CheckBoxItemRenderer()
		{
			super();
			this.addEventListener(MouseEvent.CLICK,mouseClickHandle) ;
		}

		override public function validateProperties():void
		{
			super.validateProperties();
			if (listData)
			{
				
				var dg:DataGrid = DataGrid(listData.owner);
	
				var column:CheckBoxHeaderColumn =	dg.columns[listData.columnIndex];
				column.addEventListener("click",columnHeaderClickHandler);

				if (data[column.dataField] == "true") {
				    selected = true ;
				} else {
					data[column.dataField] = false ;
				    selected = false ;
				}
			}
		}
		
		private function columnHeaderClickHandler(event:MouseEvent):void
		{
			//why this alery shows three times for a data of 2 rows
			mx.controls.Alert.show("alert");
			selected = event.target.selected;
            mouseClickHandle(event);
		}

		private function mouseClickHandle(event:MouseEvent):void
		{
			//why this alery shows three times for a data of 2 rows
			//mx.controls.Alert.show(listData + "alert");
			if (listData)
			{	
				var dg:DataGrid = DataGrid(listData.owner);
	
				var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
				//data[column.dataField] = selected ;
				data[column.dataField] = selected ;
				//dg.dataProvider.itemUpdated(data,column.dataField);
				
			}

		}
		
	}
}